<?php
use Phalcon\Cli\Task;
use Common\Common;

class OrderpullTask extends Task
{
    public function orderpullAction($params){
        if(isset($params) && !empty($params)){
            $syncName = $params;
        }else{
            $syncName = 'taobaoMonth';
        }
        $result = $this->platformOrder($syncName);
    }


    private function platformOrder($syncName){
        /**
         * 拉单
         * @param $params
         * @return array
         */
        $lastLastMont['start'] = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') - 2, 1, date('Y')));
        $lastLastMont['end'] = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));

        $lastMonth['start'] = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
        $lastMonth['end'] = date('Y-m-d H:i:s', mktime(23, 59, 59, date('m') - 0, 1 - 1, date('Y')));

        $month['start'] = date('Y-m') . "-01 00:00:00";
        $month['end'] = date('Y-m-d H:i:s');

        $today['start'] = date('Y-m-d') . ' 00:00:00';
        $today['end'] = date('Y-m-d H:i:s');

        $min['start'] = date("Y-m-d H:i:s", time() - 18 * 60);
        $min['end'] = date("Y-m-d H:i:s");

        switch ($syncName) {
            case "taobaoLastLastMonth": //淘宝上上个月订单
                return $this->taobaoGetOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end']);
                break;
            case "taobaoLastMonth": //淘宝上个月订单
                return $this->taobaoGetOrderTime($syncName, $lastMonth['start'], $lastMonth['end']);
                break;
            case "taobaoMonth":     //淘宝本月
                return $this->taobaoGetOrderTime($syncName, $month['start'], $month['end']);
                break;
            case "taobao2Month":    //淘宝本月
                return $this->taobaoGetOrderTime($syncName, $month['start'], $month['end']);
                break;
            case "taobaoToday":     //淘宝本月
                return $this->taobaoGetOrderTime($syncName, $today['start'], $today['end']);
                break;
            case "taobaomin":       //淘宝每分钟
                return $this->taobaoGetOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case "jdLastLastMonth":
                return $this->jdGetOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end']);
                break;
            case "jdLastMonth":
                return $this->jdGetOrderTime($syncName, $lastMonth['start'], $lastMonth['end']);
                break;
            case "jdMonth":
                return $this->jdGetOrderTime($syncName, $month['start'], $month['end']);
                break;
            case "jdToday":
                return $this->jdGetOrderTime($syncName, $today['start'], $today['end']);
                break;
            case "jdmin":
                return $this->jdGetOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case 'pddLastLastMonth':
                return $this->pddGetOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end']);
                break;
            case 'pddLastMonth':
                return $this->pddGetOrderTime($syncName, $lastMonth['start'], $lastMonth['end']);
                break;
            case "pddMonth":
                return $this->pddGetOrderTime($syncName, $month['start'], $month['end']);
                break;
            case "pddToday":
                return $this->pddGetOrderTime($syncName, $today['start'], $today['end']);
                break;
            case "pddmin":
                return $this->pddGetOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case "suningmin":
                return $this->suningGetOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case "suningToday":
                return $this->suningGetOrderTime($syncName, $today['start'], $today['end']);
                break;
            case "suningMonth":
                return $this->suningGetOrderTime($syncName, $month['start'], $month['end']);
                break;
            case "vphmin":
                return $this->vphGetOrderTime($syncName, $min['start'], $min['end'], false);//错误修复
                break;
            case "vphToday":
                return $this->vphGetOrderTime($syncName, $today['start'], $today['end']);
                break;
            case "vphMonth":
                return $this->vphGetOrderTime($syncName, $month['start'], $month['end']);
                break;
            case "vphLastLastMonth":
                return $this->vphGetOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end']);
                break;
            case "meituanMonth":
                return $this->meituanGetOrderTime($syncName, $month['start'], $month['end'], true);
                return $this->meituanGetOrderTime($syncName, $month['start'], $month['end'], true, 1);
                break;
            case "meituanToday":
                return $this->meituanGetOrderTime($syncName, $today['start'], $today['end'], true);
                return $this->meituanGetOrderTime($syncName, $today['start'], $today['end'], true, 1);
                break;
            case "meituanLastMonth":
                return $this->meituanGetOrderTime($syncName, $lastMonth['start'], $lastMonth['end'], true);
                return $this->meituanGetOrderTime($syncName, $lastMonth['start'], $lastMonth['end'], true, 1);
                break;
            case "meituanmin":
                return $this->meituanGetOrderTime($syncName, $min['start'], $min['end'], false);
                return $this->meituanGetOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case "taobaoWgLastLastMonth":
                return $this->weigui($syncName, $lastLastMont['start'], $lastLastMont['end'], true);
                break;
            case "taobaoWgLastMonth":
                return $this->weigui($syncName, $lastMonth['start'], $lastMonth['end'], true);
                break;
            case "taobaoWgMonth":
                return $this->weigui($syncName, $month['start'], $month['end'], true);
                break;
            case "taobaoWqLastLastMonth":
                return $this->weiquan($syncName, $month['start'], $month['end'], true);
                break;
            case "taobaoWqLastMonth":
                return $this->weiquan($syncName, $month['start'], $month['end'], true);
                break;
            case "taobaoWqMonth":
                return $this->weiquan($syncName, $month['start'], $month['end'], true);
                break;
            case "jutuikeLastLastMonth":
                return $this->jutuikeOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end'], true);
                break;
            case "jutuikeLastMonth":
                return $this->jutuikeOrderTime($syncName, $lastMonth['start'], $lastMonth['end'], true);
                break;
            case "jutuikeMonth":
                return $this->jutuikeOrderTime($syncName, $month['start'], $month['end'], true);
                break;
            case "jutuikeToday":
                return $this->jutuikeOrderTime($syncName, $today['start'], $today['end'], true);
                break;
            case "jutuikemin":
                return $this->jutuikeOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case "haodankuDyLastLastMonth":
                return $this->haodankuDyOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end'], true);
                break;
            case "haodankuDyLastMonth":
                return $this->haodankuDyOrderTime($syncName, $lastMonth['start'], $lastMonth['end'], true);
                break;
            case "haodankuDyMonth":
                return $this->haodankuDyOrderTime($syncName, $month['start'], $month['end'], true);
                break;
            case "haodankuDyToday":
                return $this->haodankuDyOrderTime($syncName, $today['start'], $today['end'], true);
                break;
            case "haodankuDymin":
                return $this->haodankuDyOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            case "meiTuanJuTuiKeLastLastMonth":
                return $this->jutuikeMeiTuanOrderTime($syncName, $lastLastMont['start'], $lastLastMont['end'], true);
                break;
            case "meiTuanJuTuiKeLastMonth":
                return $this->jutuikeMeiTuanOrderTime($syncName, $lastMonth['start'], $lastMonth['end'], true);
                break;
            case "meiTuanJuTuiKeMonth":
                return $this->jutuikeMeiTuanOrderTime($syncName, $month['start'], $month['end'], true);
                break;
            case "meiTuanJuTuiKeToday":
                return $this->jutuikeMeiTuanOrderTime($syncName, $today['start'], $today['end'], true);
                break;
            case "meiTuanJuTuiKeMin":
                return $this->jutuikeMeiTuanOrderTime($syncName, $min['start'], $min['end'], false);
                break;
            default:
                return "";
                break;
        }
        return "";
    }




    /**
     * 淘宝订单
     */
    public function taobaoGetOrderTime($syncType, $start, $end, $flg = true)
    {
        if ($flg) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
        } else {
            $orderSync['end_time'] = $start;
            $data['end_time'] = $end;
        }
        $responseData = $this->zhetaoke->getOrder($orderSync['end_time'], $data['end_time']);      //渠道订单
        $responseDataAll = $this->zhetaoke->getOrder($orderSync['end_time'], $data['end_time'], "", 1, 1);    //常规订单
        if ($responseDataAll != []) {
            $temp_data = [];
            if (!empty($responseData)) {
                foreach ($responseData as $val) {
                    $temp_data[$val['orderId']] = $val;
                }
            }
            foreach ($responseDataAll as $value) {
                if (!isset($temp_data[$value['orderId']])) {
                    $responseData[] = $value;
                }
            }
        }
        if ($responseData) {
            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("tb_rid",$responseData);
//            (new \Common\Models\Orders())->unionOrderInsert('tb_rid', $responseData);
            return "淘宝订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return '空';
    }


    /**
     * 京东订单
     */
    public function jdGetOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                if ($orderSync['hour'] == 'day') {
                    $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + 24 * 3600);
                } elseif ($orderSync['hour'] == 'hour') {
                    $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + 3600);
                } else {
                    $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 3600);
                }
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
            $type = "addOrderTime";
        } else {
            $data['end_time'] = date("Y-m-d H:i:00", time() - 60);
            $orderSync['hour'] = "min";
            $type = "updateOrderTime";
        }
        $responseData = $this->jd->getOrder($data['end_time'], $orderSync['hour'], $type);
        if ($responseData) {
            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("jd_rid",$responseData);
//            var_dump("京东订单-{$syncType}，一共获取" . count($responseData) . "条数据");
            return "京东订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }


    /**
     * 拼多多订单
     */
    public function pddGetOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
        } else {
            $orderSync['end_time'] = date("Y-m-d H:i:s", time() - 5 * 60);
            $data['end_time'] = date("Y-m-d H:i:s");
        }
        $responseData = $this->pdd->getOrder($orderSync['end_time'], $data['end_time']);
        if ($responseData) {
            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("pdd_pid",$responseData);
            var_dump("拼多多订单-{$syncType}，一共获取" . count($responseData) . "条数据");
        }
        return "空";
    }


    /**
     * 唯品会订单
     */
    public function vphGetOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
        } else {
            $orderSync['end_time'] = (strtotime(date("Y-m-d H:i:00")) - 600) * 1000;
            $data['end_time'] = (strtotime(date("Y-m-d H:i:59")) + 30) * 1000;
        }
        $responseData = $this->vph->getOrder($orderSync['end_time'], $data['end_time']);
        if ($responseData) {

            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("vph_rid",$responseData);

            return "唯品会订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }


    /**
     * 苏宁订单
     * https://open.suning.com/ospos/apipage/toApiMethodDetailMenuNew.do?interCode=suning.netalliance.order.query
     */
    public function suningGetOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > date("Y-m-d H:i:s")) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
        } else {
            $orderSync['end_time'] = date("Y-m-d H:i:00", time() - 120);
            $data['end_time'] = date("Y-m-d H:i:59", time() + 30);
        }
        $responseData = $this->suning->getOrder($orderSync['end_time'], $data['end_time']);
        if ($responseData) {

            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("sn_rid",$responseData);

            return "苏宁订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }


    /**
     * 美团订单
     * https://union.meituan.com/v2/apiDetail?id=23
     */
    public function meituanGetOrderTime($syncType, $start, $end, $flag = true, $osType = "2")
    {
        if ($flag) {
            $orderSync = $this->tGoodsOrderSycn->getSrcTypeTime($syncType);
            if (empty($orderSync)) {
                return [];
            }
            if ($orderSync['end_time'] > date("Y-m-d H:i:s")) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $this->tGoodsOrderSycn->updateEndTime($orderSync['sync_id'], $data);
        } else {
            $orderSync['end_time'] = date("Y-m-d H:i:00", time() - 120);
            $data['end_time'] = date("Y-m-d H:i:59", time() + 30);
        }
        $responseData1 = $this->meituan->getOrder($orderSync['end_time'], $data['end_time'], 1, 1, [], $osType);
        if ($responseData1) {
//            var_dump("美团外卖订单-{$syncType}，一共获取" . count($responseData1) . "条数据");
            return "美团外卖订单-{$syncType}，一共获取" . count($responseData1) . "条数据";
        }

        $responseData2 = $this->meituan->getOrder($orderSync['end_time'], $data['end_time'], 1, 2, [], $osType);
        if ($responseData2) {
//            var_dump("美团订单-{$syncType}，一共获取" . count($responseData2) . "条数据");
            return "美团订单-{$syncType}，一共获取" . count($responseData2) . "条数据";
        }

        $responseData3 = $this->meituan->getOrder($orderSync['end_time'], $data['end_time'], 1, 3, [], $osType);
        if ($responseData3) {
//            var_dump("美团订单-{$syncType}，一共获取" . count($responseData3) . "条数据");
            return "美团订单-{$syncType}，一共获取" . count($responseData3) . "条数据";
        }

        $responseData4 = $this->meituan->getOrder($orderSync['end_time'], $data['end_time'], 1, 4, [], $osType);
        if ($responseData4) {
//            var_dump("美团订单-{$syncType}，一共获取" . count($responseData4) . "条数据");
            return "美团订单-{$syncType}，一共获取" . count($responseData4) . "条数据";
        }

        $responseData5 = $this->meituan->getOrder($orderSync['end_time'], $data['end_time'], 1, 5, [], $osType);
        if ($responseData5) {
//            var_dump("美团订单-{$syncType}，一共获取" . count($responseData5) . "条数据");
            return "美团订单-{$syncType}，一共获取" . count($responseData5) . "条数据";
        }
        return "空";
    }

    /**
     * 美团聚推客订单
     */
    public function jutuikeMeiTuanOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
            $type = "updateOrderTime";
        } else {
            $orderSync['end_time'] = $start;
            $data['end_time'] = $end;
            $type = "addOrderTime";
        }
        $responseData = $this->jutuike->meituanOrders($orderSync['end_time'], $data['end_time'], $type);
        if ($responseData) {

            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("jtk_rid",$responseData);

            return "聚推客美团订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }


    /**
     * 聚推客订单
     */
    public function jutuikeOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $FOrderSync = (new \Common\Models\FOrderSync())->findFirstByTypeName($syncType);
            if (empty($FOrderSync)) {
                return [];
            }
            $orderSync = $FOrderSync->toarray();
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $FOrderSync->end_time = $data['end_time'];
            $FOrderSync->save();
            $type = "updateOrderTime";
        } else {
            $orderSync['end_time'] = $start;
            $data['end_time'] = $end;
            $type = "addOrderTime";
        }
        $responseData = $this->jtk->getOrder($orderSync['end_time'], $data['end_time'], $type);
        if ($responseData) {
            $wOrderSettle = new \Common\Models\WOrderSettle();
            $wOrderSettle->insertUpdateUnionUserOrder("jtk_rid",$responseData);

            return "聚推客订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }


    /**
     * 好单库抖音
     */
    public function haodankuDyOrderTime($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $orderSync = $this->tGoodsOrderSycn->getSrcTypeTime($syncType);
            if (empty($orderSync)) {
                return [];
            }
            if ($orderSync['end_time'] > $end) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $this->tGoodsOrderSycn->updateEndTime($orderSync['sync_id'], $data);
        } else {
            $orderSync['end_time'] = $start;
            $data['end_time'] = $end;
        }
        $responseData_material = $this->haodanku->getOrder($orderSync['end_time'], $data['end_time']);      //物料订单
        $responseData_live = $this->haodanku->getOrder($orderSync['end_time'], $data['end_time'], $page_no = 1, $media_type = 2);   //直播订单
        if($responseData_material && $responseData_live){
            $responseData = array_merge($responseData_material,$responseData_live);
        }elseif ($responseData_material){
            $responseData = $responseData_material;
        }else{
            $responseData = $responseData_live;
        }
        if ($responseData) {
            return "好单库抖音订单-{$syncType}，一共获取".count($responseData)."条数据";
        }
        return "空";
    }

    /**
     * 淘宝违规订单
     */
    public function weigui($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $orderSync = $this->tGoodsOrderSycn->getSrcTypeTime($syncType);
            if (empty($orderSync)) {
                return [];
            }
            if ($orderSync['end_time'] > date("Y-m-d H:i:s")) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $this->tGoodsOrderSycn->updateEndTime($orderSync['sync_id'], $data);
        } else {
            $orderSync['end_time'] = date("Y-m-d H:i:00", time() - 120);
            $data['end_time'] = date("Y-m-d H:i:59", time() + 30);
        }
        $responseData = $this->dataoke->getOutOfLineGetOrder($orderSync['end_time'], $orderSync['hour']);
        if ($responseData) {

            $this->tUnionOrderMapper->weiguiOrderDeal($responseData);

            return "淘宝违规订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }

    /**
     * 维权订单
     */
    public function weiquan($syncType, $start, $end, $flag = true)
    {
        if ($flag) {
            $orderSync = $this->tGoodsOrderSycn->getSrcTypeTime($syncType);
            if (empty($orderSync)) {
                return [];
            }
            if ($orderSync['end_time'] > date("Y-m-d H:i:s")) {
                $data['end_time'] = $start;
            } else {
                $data['end_time'] = date("Y-m-d H:i:s", strtotime($orderSync['end_time']) + $orderSync['hour'] * 60 * 60);
            }
            $this->tGoodsOrderSycn->updateEndTime($orderSync['sync_id'], $data);
        } else {
            $orderSync['end_time'] = date("Y-m-d H:i:00", time() - 120);
            $data['end_time'] = date("Y-m-d H:i:59", time() + 30);
        }
        $responseData = $this->dataoke->weiquanGetOrder($orderSync['end_time'], $orderSync['hour']);
        if ($responseData) {

            return "淘宝维权订单-{$syncType}，一共获取" . count($responseData) . "条数据";
        }
        return "空";
    }


}
